clear
*This is the main path to where the file was saved. I don't want the original file to be overwritten
*so I always create a version two of it. Thus, you'll see both below and used in each of teh graph 
*creations that follow. 

*Insert the path to where the other DoFile saved the document
use "~\exposures_descriptive_FPA.dta"
save "~\exposures_descriptive_FPA2.dta", replace
clear

******************************************************************************
*FIGURE 1 Creation and Export
*Total Exposures between 1946-2010, Manually Labeled across 3 periods - Early/Mid-Late/Post
clear
*Insert path to where you saved the file here
use "~\exposures_descriptive_FPA.dta"
save "~\exposures_descriptive_FPA2.dta", replace
clear
use "~\exposures_descriptive_FPA2.dta", replace

tsset dyad edate, daily

*Fill in gaps
tsfill
quietly replace edate = mdy(stmon,stday,styear) if edate == .
scalar ref_1950 = mdy(1,1,1950)
scalar ref_1960 = mdy(1,1,1960)
scalar ref_1963 = mdy(1,1,1963)
scalar ref_1970 = mdy(1,1,1970)
scalar ref_1980 = mdy(1,1,1980)
scalar ref_1990 = mdy(1,1,1990)
scalar ref_1991 = mdy(1,1,1991)
scalar ref_2000 = mdy(1,1,2000)
scalar ref_2010 = mdy(1,1,2010)

tssmooth ma sum_all_spying_ma111 = sum_all_spying, window(1 1 1)

twoway ///
    (tsline sum_all_spying sum_all_spying_ma, lpattern(solid)), xline(`=ref_1963', lpattern(solid) lcolor(black)) xline(`=ref_1991', lpattern(solid) lcolor(black)) title("Figure 1: Exposures 1946-2010") ytitle("Frequency of Exposures") xtitle("Year") xlabel(`=ref_1950' "1950" `=ref_1960' "1960" `=ref_1970' "1970" `=ref_1980' "1980" `=ref_1990' "1990" `=ref_2000' "2000" `=ref_2010' "2010", labsize(small)) yscale(range(0 30)) ylabel(0(5)30, labsize(small)) legend(off) scheme(s1mono) aspect(.4)

clear

******************************************************************************
*FIGURE 2 Creation and Export
clear
*Insert path to where you saved the file here
use "~\exposures_descriptive_FPA.dta"
save "~\exposures_descriptive_FPA2.dta", replace
clear
use "~\exposures_descriptive_FPA2.dta", replace

graph hbar (count) us_is_spying us_is_target, over(undirected, sort(new_identity) reverse descending) legend(off) title("Figure 2: Exposure Count by State, 1946-2010") scheme(s1mono) blabel(bar, format(%9.0g)) yscale(range(0 100)) ylabel(0(10)100) legend(on order(2 "US is Target" 1 "US is Offender") position(4) ring(0) cols(1) region(lstyle(none)))
graph export "C:\Users\CIA\Desktop\Dissertation\HEX Project\Evolving Dataset\Models, Code, and Papers\HEX Papers\Graphs and Tables\Figure2_FPA.png", width(1200) replace

clear

******************************************************************************
*FIGURE 3 Creation and Export
clear
*Insert path to where you saved the file here
use "~\exposures_descriptive_FPA.dta"
save "~\exposures_descriptive_FPA2.dta", replace
clear
use "~\exposures_descriptive_FPA2.dta", replace


graph hbar (count) us_is_spying us_is_target, over(reason, reverse descending) legend(off) title("Figure 3: Motivation of Accused (MICE)") scheme(s1mono) blabel(bar, format(%9.0g)) yscale(range(0 50)) ylabel(0(10)50) legend(on order(2 "US is Target" 1 "US is Offender") position(4) ring(0) cols(1) region(lstyle(none)))
graph export "C:\Users\CIA\Desktop\Dissertation\HEX Project\Evolving Dataset\Models, Code, and Papers\HEX Papers\Graphs and Tables\Figure3_FPA.png", width(1200) replace

clear

******************************************************************************
*FIGURE 4 Creation and Export
clear
*Insert path to where you saved the file here
use "~\exposures_descriptive_FPA.dta"
save "~\exposures_descriptive_FPA2.dta", replace
clear
use "~\exposures_descriptive_FPA2.dta", replace

gen profession_order = .
	replace profession_order = 1 if profession == 9   // Other Government Employee (TOP)
	replace profession_order = 2 if profession == 1   // Aid Worker
	replace profession_order = 3 if profession == 4   // Journalist
	replace profession_order = 4 if profession == 5   // Religious
	replace profession_order = 5 if profession == 3   // Business/Engineer
	replace profession_order = 6 if profession == 2   // Tourist/Academic
	replace profession_order = 7 if profession == 7   // Police/Intelligence
	replace profession_order = 8 if profession == 6   // Diplomat
	replace profession_order = 9 if profession == 8   // Current/Former Military (BOTTOM)

graph hbar (count) tot_spying, over(profession, sort(profession_order) label(labsize(small) notick) gap(50)) title("Figure 4: Frequency of Exposures by Profession") scheme(s1mono) blabel(bar, format(%9.0g)) yscale(range(0 80)) ylabel(0(10)80, grid gstyle(dash)) ytitle("")
graph export "C:\Users\CIA\Desktop\Dissertation\HEX Project\Evolving Dataset\Models, Code, and Papers\HEX Papers\Graphs and Tables\Figure4_FPA.png", width(1200) replace

clear

******************************************************************************
*FIGURE 5 Creation and Export NOT YET WORKING
clear
*Insert path to where you saved the file here
use "~\exposures_descriptive_FPA.dta"
save "~\exposures_descriptive_FPA2.dta", replace
clear
use "~\exposures_descriptive_FPA2.dta", replace

gen sortby = .
	replace sortby = 1 if identity5 == 365710
	replace sortby = 2 if identity5 == 2365
	replace sortby = 3 if identity5 == 2710

graph bar other_govt journalist clergy business_engineer tourist_academic intelligence diplomat military, ///
    over(rus_chn_us, sort(sortby) reverse descending) percent ///
    bar(8, color(black)) ///
    bar(7, color(gs3)) ///
    bar(6, color(gs5)) ///
    bar(5, color(gs7)) ///
    bar(4, color(gs9)) ///
    bar(3, color(gs11)) ///
    bar(2, color(gs13)) ///
    bar(1, color(gs10)) ///
    ylabel(none) ytitle("") ///
	legend(order(8 "Current / Former Military" 7 "Diplomat" 6 "Police/ Intelligence" ///
                 5 "Tourist / Academic" 4 "Business / Engineer" 3 "Religious" ///
                 2 "Journalist" 1 "Other Government") ///
           ring(0) position(12) col(3) colgap(1) size(small) symxsize(2) symysize(2) ///
           region(lcolor(none) lwidth(none) color(none))) /// Transparent background, reduced column spacing
    title("Figure 5: Percent Exposures by Profession" ///
          "for Target States US, Russia, & China", size(large)) ///
    b1title(" ") /// Creates whitespace below the title for the legend
    scheme(s1mono) ///
    blabel(bar, format(%9.1f) position(outside) size(vsmall) angle(90)) ///
    graphregion(margin(large) color(white)) /// Adds whitespace above the graph
    plotregion(style(none))


*military diplomat intelligence tourist_academic business_engineer clergy journalist other_govt
*other_govt journalist clergy business_engineer tourist_academic intelligence diplomat military

*Insert path to export location for the graph below
graph export "~\Figure5_FPA.png", width(1200) replace

clear

******************************************************************************
*FIGURE 6 Creation and Export
clear
*Insert path to where you saved the file here
use "~\exposures_descriptive_FPA.dta"
save "~\exposures_descriptive_FPA2.dta", replace
clear
use "~\exposures_descriptive_FPA2.dta", replace

label define usa_labels 99 "United States", modify
replace usa_accused = 99 if us_target == 1  // Assign a unique number to US
label values usa_accused usa_labels  // Apply the label

gen usa_accused_order = .
replace usa_accused_order = 1 if usa_accused == 99  // United States
replace usa_accused_order = 2 if usa_accused == 2   // Russia
replace usa_accused_order = 3 if usa_accused == 1   // Czechoslovakia
replace usa_accused_order = 4 if usa_accused == 4   // East Germany
replace usa_accused_order = 5 if usa_accused == 5   // China
replace usa_accused_order = 6 if usa_accused == 3   // Poland
replace usa_accused_order = 7 if usa_accused == 11  // North Korea
replace usa_accused_order = 8 if usa_accused == 9   // Romania
replace usa_accused_order = 9 if usa_accused == 13  // Iraq
replace usa_accused_order = 10 if usa_accused == 6  // Cuba
replace usa_accused_order = 11 if usa_accused == 8  // Bulgaria
replace usa_accused_order = 12 if usa_accused == 7  // Hungary
replace usa_accused_order = 13 if usa_accused == 12 // Albania
replace usa_accused_order = 14 if usa_accused == 10 // Iran
replace usa_accused_order = 15 if usa_accused == 14 // All Other States

label define usa_accused_order_labels ///
    1 "United States" ///
    2 "Russia" ///
    3 "Czechoslovakia" ///
    4 "East Germany" ///
    5 "China" ///
    6 "Poland" ///
    7 "North Korea" ///
    8 "Romania" ///
    9 "Iraq" ///
    10 "Cuba" ///
    11 "Bulgaria" ///
    12 "Hungary" ///
    13 "Albania" ///
    14 "Iran" ///
    15 "All Other States"

label values usa_accused_order usa_accused_order_labels
gen usa_accused_sort = usa_accused_order

gen has_death = 1 if (death_sentence > 0)
gen has_life = 1 if (life_sentence > 0)
gen has_expelled = 1 if (expelled > 0)
gen has_released = 1 if (released > 0)

graph hbar (sum) has_death has_life has_expelled has_released, over(usa_accused_order, sort(usa_accused_sort) reverse descending) legend(on order(4 "Released/Acquittal" 3 "Expulsion" 2 "Life" 1 "Death") position(4) ring(0) cols(1) symxsize(2) symysize(2) region(lstyle(none))) bar(1, color(black)) bar(2, color(gs6)) bar(3, color(gs10)) bar(4, color(gs14)) title("Figure 6: Frequency of Exposures that Include a Sentence of" "Death, Life, Expulsion, or Release") scheme(s1mono) yscale(range(0 25)) ylabel(0(5)25)
		     
*Insert path here for export		   
graph export "~\Figure6_FPA.png", width(1200) replace
































